package com.xyzboom.web.servlets.login;

import com.xyzboom.beans.Login;
import com.xyzboom.utils.VerifyCode;
import com.xyzboom.web.services.LoginService;
import com.xyzboom.utils.WebUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet(urlPatterns = "/servlets/login/LoginController", name = "LoginController")
public class LoginController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        if (username == null) {
            WebUtils.forward("/login.jsp", req, resp);
            return;
        }
        VerifyCode verifyCode = (VerifyCode) req.getSession().getAttribute("verify_code");
        String verify_code = req.getParameter("verify_code");
        if (verifyCode == null) {
            toFailurePage(req, resp, "验证码已经过期！");
        } else if (!verifyCode.getCode().equals(verify_code)) {
            toFailurePage(req, resp, "验证码错误！");
        }
        String password = req.getParameter("password");

        String college = req.getParameter("college");
        String major = req.getParameter("major");
        Login login = new Login(username, password, verify_code, college, major);
        req.setAttribute("login", login);
        if (username.equals("")) {
            toFailurePage(req, resp, "用户名为空！");
        }
        if (password.equals("")) {
            toFailurePage(req, resp, "密码为空！");
        }
        if (major.equals("") || major.equals("--请选择--")) {
            toFailurePage(req, resp, "没有选择专业！");
        }
        if (college.equals("")) {
            toFailurePage(req, resp, "没有选择学院！");
        }
        try {
            LoginService.LoginResult result = LoginService.login(login);
            switch (result.getLoginType()) {
                case SUCCESS:
                    req.getSession().setAttribute("user", result.getUser());
                    WebUtils.forward("/pages/main.jsp", req, resp);
                    break;
                case NO_USER:
                    toFailurePage(req, resp, "用户名不存在！");
                    break;
                case PASSWORD_ERROR:
                    toFailurePage(req, resp, "密码错误！");
                    break;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            toFailurePage(req, resp, "数据库异常");
        }
    }

    private void toFailurePage(HttpServletRequest req, HttpServletResponse resp, String reason) throws ServletException, IOException {
        req.setAttribute("reason", reason);
        WebUtils.forward("/pages/login/loginFailure.jsp", req, resp);
    }

}
